package com.supermarket.manage.common.validation;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.math.BigDecimal;

/**
 * @author Faxon
 * @version 1.0
 * @date 2020-03-12 19:45
 **/
public class DiscountValidator implements ConstraintValidator<Discount, BigDecimal> {

    public static BigDecimal min = new BigDecimal(0);
    public static BigDecimal max = new BigDecimal(10);

    private boolean require = false;

    @Override
    public void initialize(Discount constraintAnnotation) {
        require = constraintAnnotation.required();
    }

    @Override
    public boolean isValid(BigDecimal dis, ConstraintValidatorContext constraintValidatorContext) {
        if (dis == null){
            return false;
        }
        if (require) {
            return dis.compareTo(min) > 0 && dis.compareTo(max) <= 0;
        }
        return false;
    }
}
